package com.yang.jxj.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yang.jxj.entity.*;
import com.yang.jxj.service.impl.ScholarshipServiceImpl;
import com.yang.jxj.service.impl.ScoreServiceImpl;
import com.yang.jxj.service.impl.StudentsServiceImpl;
import com.yang.jxj.service.impl.UserServiceImpl;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yang
 * @since 2024-06-25
 */
@RestController
@RequestMapping("/api/scholarship")
public class ScholarshipController {
    @Autowired
    private ScholarshipServiceImpl schol;
    @Autowired
    private ScoreServiceImpl sco;
    @Autowired
    private StudentsServiceImpl stu;

    @PostMapping("/add")
    public HashMap<String, Object> addScholarship(@RequestBody Scholarship scholarship) {
        System.out.println(scholarship);
        HashMap<String, Object> map = new HashMap<>();
        scholarship.setStatus("待审核");
        Score scoById = sco.getById(scholarship.getStudentId());
        Students stuById = stu.getById(scholarship.getStudentId());
        scholarship.setScore(String.valueOf(scoById.getTotal()));
        scholarship.setGrade(stuById.getGrade());
        boolean b = schol.saveOrUpdate(scholarship);
        if (b) {
            map.put("code", 0);
            map.put("msg", "申请成功");
            return map;
        } else {
            map.put("code", 1);
            map.put("msg", "申请失败");
            return map;
        }

    }

    @GetMapping("/msg")
    public ResponseEntity<IPage<Scholarship>> msg(HttpServletRequest request) {
        Integer userId = Integer.valueOf(String.valueOf(request.getAttribute("userId")));
        Page<Scholarship> page = new Page<>(1, 1);
        QueryWrapper<Scholarship> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("student_id", userId);

        return ResponseEntity.ok(schol.page(page, objectQueryWrapper));
    }

    @GetMapping("/all")
    public IPage<Scholarship> findPage1(@RequestParam Integer pageNum,
                                        @RequestParam Integer pageSize,
                                        @RequestParam String studentId,
                                        @RequestParam String status) {
        Page<Scholarship> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Scholarship> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.like("student_id", studentId);
        objectQueryWrapper.like("status", status);
        objectQueryWrapper.orderByAsc("student_id");
        return schol.page(page, objectQueryWrapper);
    }
    @GetMapping("/change")
    public HashMap<String, Object> changeScholarship(@RequestParam Integer studentId) {
        HashMap<String, Object> map = new HashMap<>();
        Scholarship scholById = schol.getById(studentId);
        scholById.setStatus("审核通过");
        boolean b = schol.saveOrUpdate(scholById);
        if (b) {
            map.put("code", 0);
            map.put("msg", "审核通过");
            return map;
        } else {
            map.put("code", 1);
            map.put("msg", "审核失败");
            return map;
        }
    }

    @PostMapping("/return")
    public HashMap<String, Object> returnScholarship(@RequestBody Scholarship scholById) {
        HashMap<String, Object> map = new HashMap<>();
         scholById = schol.getById(scholById.getStudentId());
        scholById.setStatus("未通过");
        boolean b = schol.saveOrUpdate(scholById);
        if (b) {
            map.put("code", 0);
            map.put("msg", "驳回通过");
            return map;
        } else {
            map.put("code", 1);
            map.put("msg", "驳回失败");
            return map;
        }
    }
    @GetMapping("/finish")
    public IPage<Scholarship> findPage(@RequestParam Integer pageNum,
                                        @RequestParam Integer pageSize,
                                        @RequestParam String studentId,
                                        @RequestParam String grade
                                        ) {
        Page<Scholarship> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Scholarship> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.like("student_id", studentId);
        objectQueryWrapper.like("grade",grade);
        objectQueryWrapper.eq("status","审核通过");
        objectQueryWrapper.orderByAsc("score");
        return schol.page(page, objectQueryWrapper);
    }

    @PostMapping("/update")
    public HashMap<String,Object> saorup(@RequestBody Scholarship scholarship) {
        HashMap<String, Object> map = new HashMap<>();
        boolean b = schol.saveOrUpdate(scholarship);
        if(b){
            map.put("code",0);
            map.put("msg","成功");
            return map;
        }else{
            map.put("code",1);
            map.put("msg","失败");
            return map;
        }
    }
    @PostMapping("/del")
    public HashMap<String, Object> del(@RequestBody Scholarship scholarship) {
        HashMap<String, Object> map = new HashMap<>();

        QueryWrapper<Scholarship> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("student_id", scholarship.getStudentId());

        boolean remove = schol.remove(objectQueryWrapper);
        if (remove) {
            map.put("code", 0);
            map.put("msg", "删除成功");
            return map;
        } else {
            map.put("code", 1);
            map.put("msg", "删除失败");
            return map;
        }
    }
}